<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 4.2.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"yoursite.com","root":"/","scheme":"Gemini","version":"7.7.2","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}}};
  </script>

  <meta property="og:type" content="website">
<meta property="og:title" content="Roderick&#39;s blog">
<meta property="og:url" content="http://yoursite.com/index.html">
<meta property="og:site_name" content="Roderick&#39;s blog">
<meta property="og:locale" content="zh_CN">
<meta property="article:author" content="Roderick Xiang">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="http://yoursite.com/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : true,
    isPost : false
  };
</script>

  <title>Roderick's blog</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <div>
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Roderick's blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>


<nav class="site-nav">
  
  <ul id="menu" class="menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-fw fa-home"></i>首页</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-fw fa-archive"></i>归档</a>

  </li>
  </ul>

</nav>
</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content">
            

  <div class="posts-expand">
        
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block home" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2020/03/13/%E7%94%A8%E6%88%B7%E4%BF%A1%E6%81%AF%E5%88%97%E8%A1%A8%E5%B1%95%E7%A4%BA/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="Roderick Xiang">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Roderick's blog">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          
            <a href="/2020/03/13/%E7%94%A8%E6%88%B7%E4%BF%A1%E6%81%AF%E5%88%97%E8%A1%A8%E5%B1%95%E7%A4%BA/" class="post-title-link" itemprop="url">用户信息列表展示</a>
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2020-03-13 20:51:49" itemprop="dateCreated datePublished" datetime="2020-03-13T20:51:49+08:00">2020-03-13</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2020-03-17 21:55:19" itemprop="dateModified" datetime="2020-03-17T21:55:19+08:00">2020-03-17</time>
              </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
          <!-- TOC -->

<ul>
<li><a href="#整体框架">整体框架</a></li>
<li><a href="#用户数据">用户数据</a></li>
<li><a href="#重要功能实现">重要功能实现</a><ul>
<li><a href="#管理员登入">管理员登入</a><ul>
<li><a href="#过滤器filter">过滤器(Filter)</a></li>
</ul>
</li>
<li><a href="#分页查询所有用户数据">分页查询所有用户数据</a></li>
<li><a href="#增加新的数据">增加新的数据</a></li>
<li><a href="#更改用户数据">更改用户数据</a></li>
<li><a href="#删除用户数据">删除用户数据</a><ul>
<li><a href="#单独删除">单独删除</a></li>
<li><a href="#多选删除">多选删除</a></li>
</ul>
</li>
<li><a href="#分页查询用户数据">分页查询用户数据</a><ul>
<li><a href="#sql语句">SQL语句</a></li>
<li><a href="#核心实现">核心实现</a></li>
</ul>
</li>
<li><a href="#导航条">导航条</a></li>
</ul>
</li>
</ul>
<!-- /TOC -->
<h2 id="整体框架"><a href="#整体框架" class="headerlink" title="整体框架"></a>整体框架</h2><p>为了保证不滥用jsp所以采用三层框架，分别为页面展示层，逻辑操作层，和数据库访问层(Database Access Object)，其实简单的增删改查的操作都可以交给数据库，所以基本上没有什么逻辑操作。<br><br>对于数据的操作为了在未来更好的添加功能使用实现接口的方式，UserServices和AdministratorService<br></p>
<h2 id="用户数据"><a href="#用户数据" class="headerlink" title="用户数据"></a>用户数据</h2><p>对于查询的数据使用User对象的方式进行封装，在直接获取属性就行<br></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">private</span> <span class="keyword">int</span> id;</span><br><span class="line"><span class="keyword">private</span> String name;</span><br><span class="line"><span class="keyword">private</span> String gender;</span><br><span class="line"><span class="keyword">private</span> <span class="keyword">int</span> age;</span><br><span class="line"><span class="keyword">private</span> String address;</span><br><span class="line"><span class="keyword">private</span> String qq;</span><br><span class="line"><span class="keyword">private</span> String email;</span><br></pre></td></tr></table></figure>
<h2 id="重要功能实现"><a href="#重要功能实现" class="headerlink" title="重要功能实现"></a>重要功能实现</h2><h3 id="管理员登入"><a href="#管理员登入" class="headerlink" title="管理员登入"></a>管理员登入</h3><pre class="mermaid">graph LR
浏览器发起request请求-->servlet{Servlet进行判断}
servlet{Servlet进行判断}--有loginAdministrator返回值-->允许访问index.jsp
servlet{Servlet进行判断}--没有,转发错误信息-->login.jsp</pre>
<p>登入在数据库中需要一张管理员的表，同样封装管理员对象<br></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">private</span> String username;</span><br><span class="line"><span class="keyword">private</span> String password;</span><br></pre></td></tr></table></figure>
<p>首先通过jsp页面展示html和验证码（通过CheckCodeServlet获取），老样子还是先判断激活码。<br></p>
<h4 id="过滤器-Filter"><a href="#过滤器-Filter" class="headerlink" title="过滤器(Filter)"></a>过滤器(Filter)</h4><p>当实现登入功能的时候就会惊奇的发现，可以直接输入别的网址就能跳过登入界面了，所以在这里需要一个Filter，它会检查所有访问的请求<br><br>1.排除登入所需要的资源（servlet jsp js之类的资源）<br><br>2.检查请求的session中是否含有user这个属性<br></p>
<pre class="mermaid">graph TD
    A[来自浏览器的请求] --> C{Fliter进行拦截是否含有user属性}
    C -->|有| D[放行]
    C -->|无| E[转发到登入页面]</pre>
<h3 id="分页查询所有用户数据"><a href="#分页查询所有用户数据" class="headerlink" title="分页查询所有用户数据"></a>分页查询所有用户数据</h3><p><strong>SQL:selset * from person_data</strong>（不完整）<br><br>由于要实现分页的要求所以前端与后端要进行一些数据的交换以达到效果，这里通过封装一个pageBean<br></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">private</span> <span class="keyword">int</span> totalCount; <span class="comment">//总记录数  （数据库）</span></span><br><span class="line"><span class="keyword">private</span> <span class="keyword">int</span> totalPage;  <span class="comment">//总页码数  （数据库）</span></span><br><span class="line"><span class="keyword">private</span> List&lt;T&gt; list;  <span class="comment">//每页的数据 （数据库返回）</span></span><br><span class="line"><span class="keyword">private</span> <span class="keyword">int</span> currentPage;    <span class="comment">//当前的页码    （前端）</span></span><br><span class="line"><span class="keyword">private</span> <span class="keyword">int</span> rows;   <span class="comment">//每页显示的记录数  （servlet规定）</span></span><br></pre></td></tr></table></figure>
<p>通过这些数据就能得知每一页应该显示什么内容<br><br>这里稍微画一下图表明一下三层架构：</p>
<pre class="mermaid">graph LR
    Servlet-->UserService
    UserService-->UserDao</pre>
<h3 id="增加新的数据"><a href="#增加新的数据" class="headerlink" title="增加新的数据"></a>增加新的数据</h3><p><strong>SQL:insert into person_data values(null,?,?,?,?,?,?)</strong><br></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">String sql = <span class="string">"insert into person_data values(null,?,?,?,?,?,?)"</span>;</span><br><span class="line">jdbcTemplate.update(sql,</span><br><span class="line">        newUser.getName(), newUser.getGender(), newUser.getAge(), newUser.getAddress(), newUser.getQq(), newUser.getEmail());</span><br></pre></td></tr></table></figure>
<p>在这里只需要获取来自前端表单提交的数据，再提交给服务器就行<br><br>直接使用BeanUtils做封装，减少代码量<br></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">Map&lt;String, String[]&gt; map = request.getParameterMap();</span><br><span class="line">User newUser = <span class="keyword">new</span> User();</span><br><span class="line"><span class="comment">//自动封装</span></span><br><span class="line"><span class="keyword">try</span> &#123;</span><br><span class="line">    BeanUtils.populate(newUser, map);</span><br><span class="line">&#125; <span class="keyword">catch</span> (IllegalAccessException | InvocationTargetException e) &#123;</span><br><span class="line">    e.printStackTrace();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="更改用户数据"><a href="#更改用户数据" class="headerlink" title="更改用户数据"></a>更改用户数据</h3><p><strong>SQL:update person_data set name = ?,gender = ?,age = ?,address = ?,qq = ?,email = ? where id = ?</strong><br></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">updateUser</span><span class="params">(User user)</span> </span>&#123;</span><br><span class="line">    String sql = <span class="string">"update person_data set name = ?,gender = ?,age = ?,address = ?,qq = ?,email = ? where id = ?"</span>;</span><br><span class="line">    jdbcTemplate.update(sql,</span><br><span class="line">            user.getName(), user.getGender(), user.getAge(), user.getAddress(), user.getQq(), user.getEmail(), user.getId());</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>这个要比新添加一个要难一些，而且要实现<strong>回显用户的数据</strong>，所以逻辑会稍微有点复杂<br></p>
<pre class="mermaid">graph LR
    list.jsp--传递id-->FindUserServlet
    FindUserServlet--转发User数据-->updata.jsp
    updata.jsp--传递User数据-->UpdataUserServlet
    UpdataUserServlet--重定项-->ListUserByPageServlet</pre>
<p>在第二次传递User数据的时候会发现表单中并没有id这个属性，所以这里耍个小聪明，在表单中隐藏id，有了id才能在数据库中做修改<br></p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">%--隐藏的id信息--%</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">"hidden"</span> <span class="attr">name</span>=<span class="string">"id"</span> <span class="attr">value</span>=<span class="string">"$&#123;requestScope.user.id&#125;"</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h3 id="删除用户数据"><a href="#删除用户数据" class="headerlink" title="删除用户数据"></a>删除用户数据</h3><p><strong>SQL:delete from person_data where id = ?</strong><br></p>
<h4 id="单独删除"><a href="#单独删除" class="headerlink" title="单独删除"></a>单独删除</h4><p>这个好说，拿到id就行<br></p>
<h4 id="多选删除"><a href="#多选删除" class="headerlink" title="多选删除"></a>多选删除</h4><p>这个功能使用了另一个DeleteSelectedUserServlet完成<br><br>实现步骤：<br><br>前端使用form表单提交本页的userId数据，判断是否被选择<br></p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">form</span> <span class="attr">action</span>=<span class="string">"$&#123;pageContext.request.contextPath&#125;/deleteSelectedUserServlet"</span> <span class="attr">method</span>=<span class="string">"post"</span> <span class="attr">id</span>=<span class="string">"form"</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">td</span>&gt;</span><span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">"checkbox"</span> <span class="attr">name</span>=<span class="string">"userId"</span> <span class="attr">value</span>=<span class="string">"$&#123;user.id&#125;"</span>&gt;</span><span class="tag">&lt;/<span class="name">td</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">form</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p>只有被勾选才能id发送到到servlet，从而在servlet端接收参数数组<br></p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="javascript">    <span class="built_in">window</span>.onload = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span></span><br><span class="line"><span class="javascript">        <span class="built_in">document</span>.getElementById(<span class="string">"deleteSelected"</span>).onclick = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span></span><br><span class="line"><span class="javascript">            <span class="keyword">let</span> ids = <span class="built_in">document</span>.getElementsByName(<span class="string">"userId"</span>);</span></span><br><span class="line"><span class="javascript">            <span class="keyword">let</span> allowToDelete = <span class="literal">false</span>;</span></span><br><span class="line"><span class="actionscript">            <span class="comment">// 判断选择框是否有勾选</span></span></span><br><span class="line"><span class="javascript">            <span class="keyword">for</span> (<span class="keyword">let</span> i = <span class="number">0</span>; i &lt; ids.length; i++) &#123;</span></span><br><span class="line"><span class="actionscript">                <span class="keyword">if</span> (ids[i].checked === <span class="literal">true</span>) &#123;</span></span><br><span class="line"><span class="actionscript">                    allowToDelete = <span class="literal">true</span>;</span></span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">            if (!allowToDelete) &#123;</span><br><span class="line"><span class="actionscript">                alert(<span class="string">"没有选中任何用户，请选择用户后提交"</span>)</span></span><br><span class="line"><span class="actionscript">            &#125; <span class="keyword">else</span> <span class="keyword">if</span> (confirm(<span class="string">"是否要删除所选吗？"</span>)) &#123;</span></span><br><span class="line"><span class="javascript">                <span class="built_in">document</span>.getElementById(<span class="string">"form"</span>).submit();</span></span><br><span class="line">            &#125;</span><br><span class="line">        &#125;;</span><br><span class="line"><span class="javascript">        <span class="built_in">document</span>.getElementById(<span class="string">"firstCheckbox"</span>).onclick = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span></span><br><span class="line"><span class="javascript">            <span class="keyword">let</span> ids = <span class="built_in">document</span>.getElementsByName(<span class="string">"userId"</span>);</span></span><br><span class="line"><span class="javascript">            <span class="keyword">for</span> (<span class="keyword">let</span> i = <span class="number">0</span>; i &lt; ids.length; i++) &#123;</span></span><br><span class="line"><span class="actionscript">                ids[i].checked = <span class="keyword">this</span>.checked;</span></span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h3 id="分页查询用户数据"><a href="#分页查询用户数据" class="headerlink" title="分页查询用户数据"></a>分页查询用户数据</h3><h4 id="SQL语句"><a href="#SQL语句" class="headerlink" title="SQL语句"></a>SQL语句</h4><p><strong>SQL:select * from person_data where 1 = 1 and key(name) like ‘%模糊查找%’ [and key(name) like ‘%模糊查找%’] limit start rows</strong><br><br>在这里又要实现用户的模糊查询，还要实现分页查询的功能就会比较麻烦<br><br>首先从sql开始说起：<br><br>1.由于模糊查询的条件有三个但是又不能写死，因为用户可以不填，而且还要进行分页，所以这里采用拼接sql的方式<br><br>2.查找的条件使用where 1 = 1 的方式这样可以直接在后面拼接，这个也算一个小技巧吧<br><br>3.由于有未知的参数，所以要一个参数列表，在里面添加参数，然后直接传入数组就行<br></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> List&lt;User&gt; <span class="title">findUserByPage</span><span class="params">(<span class="keyword">int</span> start, <span class="keyword">int</span> rows, Map&lt;String, String[]&gt; condition)</span> </span>&#123;</span><br><span class="line">    String sql = <span class="string">"select * from person_data where 1 = 1 "</span>;</span><br><span class="line">    <span class="comment">//拼接sql</span></span><br><span class="line">    StrBuilder sqlStrBuilder = <span class="keyword">new</span> StrBuilder(sql);</span><br><span class="line">    List&lt;Object&gt; params = <span class="keyword">new</span> ArrayList&lt;&gt;();    <span class="comment">//参数列表</span></span><br><span class="line">    <span class="keyword">for</span> (String key : condition.keySet()) &#123;   <span class="comment">//遍历条件</span></span><br><span class="line">        String value = condition.get(key)[<span class="number">0</span>];</span><br><span class="line">        <span class="comment">//排除不需要的参数</span></span><br><span class="line">        <span class="keyword">if</span> (<span class="string">"currentPage"</span>.equals(key) || <span class="string">"rows"</span>.equals(key)) &#123;</span><br><span class="line">            <span class="keyword">continue</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">//判断有值</span></span><br><span class="line">        <span class="keyword">if</span> (value != <span class="keyword">null</span> &amp;&amp; !<span class="string">""</span>.equals(value)) &#123;</span><br><span class="line">            sqlStrBuilder.append(<span class="string">" and "</span>).append(key).append(<span class="string">" like ? "</span>);</span><br><span class="line">            params.add(<span class="string">"%"</span> + value + <span class="string">"%"</span>);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    sqlStrBuilder.append(<span class="string">" limit ?, ? "</span>);</span><br><span class="line">    params.add(start);</span><br><span class="line">    params.add(rows);</span><br><span class="line">    <span class="keyword">return</span> jdbcTemplate.query(sqlStrBuilder.toString(), <span class="keyword">new</span> BeanPropertyRowMapper&lt;&gt;(User<span class="class">.<span class="keyword">class</span>), <span class="title">params</span>.<span class="title">toArray</span>())</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h4 id="核心实现"><a href="#核心实现" class="headerlink" title="核心实现"></a>核心实现</h4><p>这里的实现借助于分页列出所有用户的数据，在listUserByPage下添加一个condition的参数，在condition中含有 currentPage rows [name] [address] [email]<br><br>在dao中进行sql的处理需要用到condition<br></p>
<h3 id="导航条"><a href="#导航条" class="headerlink" title="导航条"></a>导航条</h3><p>上一页和下一页<br></p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">li</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">a</span> <span class="attr">href</span>=<span class="string">"$&#123;pageContext.request.contextPath&#125;/listUserByPageServlet?currentPage=$&#123;requestScope.pageBean.currentPage - 1&#125;&amp;rows=$&#123;requestScope.pageBean.rows&#125;&amp;name=$&#123;requestScope.condition.name[0]&#125;&amp;address=$&#123;requestScope.condition.address[0]&#125;&amp;email=$&#123;requestScope.condition.email[0]&#125;"</span></span></span><br><span class="line"><span class="tag">       <span class="attr">aria-label</span>=<span class="string">"Previous"</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">span</span> <span class="attr">aria-hidden</span>=<span class="string">"true"</span>&gt;</span><span class="symbol">&amp;laquo;</span><span class="tag">&lt;/<span class="name">span</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">a</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p>显示现在的页面<br></p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">c:forEach</span> <span class="attr">begin</span>=<span class="string">"1"</span> <span class="attr">end</span>=<span class="string">"$&#123;requestScope.pageBean.totalPage&#125;"</span> <span class="attr">step</span>=<span class="string">"1"</span> <span class="attr">var</span>=<span class="string">"number"</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">%--设置激活状态--%</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">c:if</span> <span class="attr">test</span>=<span class="string">"$&#123;requestScope.pageBean.currentPage==number&#125;"</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">li</span> <span class="attr">class</span>=<span class="string">"active"</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">a</span> <span class="attr">href</span>=<span class="string">"$&#123;pageContext.request.contextPath&#125;/listUserByPageServlet?currentPage=$&#123;number&#125;&amp;rows=$&#123;requestScope.pageBean.rows&#125;&amp;name=$&#123;requestScope.condition.name[0]&#125;&amp;address=$&#123;requestScope.condition.address[0]&#125;&amp;email=$&#123;requestScope.condition.email[0]&#125;"</span>&gt;</span>$&#123;number&#125;<span class="tag">&lt;/<span class="name">a</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">c:if</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">c:if</span> <span class="attr">test</span>=<span class="string">"$&#123;requestScope.pageBean.currentPage!=number&#125;"</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">li</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">a</span> <span class="attr">href</span>=<span class="string">"$&#123;pageContext.request.contextPath&#125;/listUserByPageServlet?currentPage=$&#123;number&#125;&amp;rows=$&#123;requestScope.pageBean.rows&#125;&amp;name=$&#123;requestScope.condition.name[0]&#125;&amp;address=$&#123;requestScope.condition.address[0]&#125;&amp;email=$&#123;requestScope.condition.email[0]&#125;"</span>&gt;</span>$&#123;number&#125;<span class="tag">&lt;/<span class="name">a</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">c:if</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;/<span class="name">c:forEach</span>&gt;</span></span><br></pre></td></tr></table></figure>
      
    </div>

    
    
    
      <footer class="post-footer">
        <div class="post-eof"></div>
      </footer>
  </article>
  
  
  

        
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block home" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2020/03/04/%E7%AE%80%E5%8D%95%E5%AE%9E%E7%8E%B0%E9%A1%B5%E9%9D%A2%E7%99%BB%E5%85%A5/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="Roderick Xiang">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Roderick's blog">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          
            <a href="/2020/03/04/%E7%AE%80%E5%8D%95%E5%AE%9E%E7%8E%B0%E9%A1%B5%E9%9D%A2%E7%99%BB%E5%85%A5/" class="post-title-link" itemprop="url">简单实现页面登入</a>
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>
              

              <time title="创建时间：2020-03-04 22:52:09 / 修改时间：22:50:01" itemprop="dateCreated datePublished" datetime="2020-03-04T22:52:09+08:00">2020-03-04</time>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
          <!-- TOC -->

<ul>
<li><a href="#简单实现">简单实现</a></li>
<li><a href="#整体思想">整体思想</a></li>
<li><a href="#亿点细节">亿点细节</a><ul>
<li><a href="#在登入页面下直接输出验证码错误信息">在登入页面下直接输出验证码错误信息</a></li>
<li><a href="#看不清换一张">看不清换一张</a></li>
<li><a href="#中文乱码的问题">中文乱码的问题</a></li>
</ul>
</li>
</ul>
<!-- /TOC -->
<h3 id="简单实现"><a href="#简单实现" class="headerlink" title="简单实现"></a>简单实现</h3><p>使用servlet和jsp简单写一个登入页面<br><br>实现：<br><br>1.使用MySQL实现对于用户名和密码的判断(Spring JDBCTemplate)<br><br>2.通过BufferImage实现验证码的简单绘制<br><br>3.先判断验证码，减轻服务器的无效开销<br></p>
<h3 id="整体思想"><a href="#整体思想" class="headerlink" title="整体思想"></a>整体思想</h3><p>1.通过表单获取用户发送post请求到CheckUserServlet<br><br>2.判断用户输入的验证码。使用CheckCodeServlet绘制验证码，使用session设置属性，使得其他servlet得以获取验证码<br><br>3.判断验证码<br><br>4.使用CheckUserServlet先封装一个User类<br><br>5.使用数据库连接对象连接数据库，传递用户名和密码，若正确则封装User对象，并将其返回，否则返回null（调用userDatabaseAccessObject.userLogin(loginUser)）<br><br>6.转发到成功或者失败的jsp<br></p>
<h3 id="亿点细节"><a href="#亿点细节" class="headerlink" title="亿点细节"></a>亿点细节</h3><h4 id="在登入页面下直接输出验证码错误信息"><a href="#在登入页面下直接输出验证码错误信息" class="headerlink" title="在登入页面下直接输出验证码错误信息"></a>在登入页面下直接输出验证码错误信息</h4><figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">&lt;div id=<span class="string">"warning"</span>&gt;</span><br><span class="line">    &lt;%=request.getAttribute(<span class="string">"verificationCode_error"</span>) == <span class="keyword">null</span> ? <span class="string">""</span> : request.getAttribute(<span class="string">"verificationCode_error"</span>)%&gt;</span><br><span class="line">&lt;/div&gt;</span><br></pre></td></tr></table></figure>
<p>使用三元运算符和jsp（&lt;%= 代碼%&gt;）直接显示在html位置中，在没有错误的时候显示空字符串，而不为空的时候显示错误信息<br></p>
<h4 id="看不清换一张"><a href="#看不清换一张" class="headerlink" title="看不清换一张"></a>看不清换一张</h4><figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">&lt;img src=<span class="string">"$&#123;pageContext.request.contextPath&#125;/checkCode"</span> alt=<span class="string">"验证码"</span> id=<span class="string">"verificationCode"</span>&gt;</span><br><span class="line">        &lt;a href="#" id="changeCode"&gt;看不清，换一张&lt;/a&gt;</span><br></pre></td></tr></table></figure>
<p>href中写#可以不用进行跳转<br><br>当然只使用html是不行的，还需要js的配合（绑定用户的点击）<br><br>这里有一个注意点，如果只是更改图片的属性，由于浏览器有缓存，所以验证码图片并不会发生更改。所以在请求servlet资源的时候要加上一个post请求（随便获取一下毫秒值），这样图片就会发生变化了。<br></p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="javascript">    <span class="built_in">window</span>.onload = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span></span><br><span class="line"><span class="javascript">        <span class="keyword">let</span> img = <span class="built_in">document</span>.getElementById(<span class="string">"verificationCode"</span>);</span></span><br><span class="line"><span class="actionscript">        img.onclick = <span class="function"><span class="keyword">function</span> <span class="params">()</span> </span>&#123;</span></span><br><span class="line"><span class="javascript">            img.src = <span class="string">"/checkCode?"</span> + <span class="keyword">new</span> <span class="built_in">Date</span>().getMilliseconds();</span></span><br><span class="line">        &#125;;</span><br><span class="line"><span class="javascript">        <span class="built_in">document</span>.getElementById(<span class="string">"changeCode"</span>).onclick = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span></span><br><span class="line"><span class="javascript">            img.src = <span class="string">"/checkCode?"</span> + <span class="keyword">new</span> <span class="built_in">Date</span>().getMilliseconds();</span></span><br><span class="line">        &#125;;</span><br><span class="line">    &#125;</span><br><span class="line"><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>
<h4 id="中文乱码的问题"><a href="#中文乱码的问题" class="headerlink" title="中文乱码的问题"></a>中文乱码的问题</h4><p>在使用servlet进行，response进行回复的时候要首先设置编码<br></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">response.setContentType(<span class="string">"text/html;charset=utf-8"</span>);</span><br></pre></td></tr></table></figure>
<p>而在jsp中idea已经进行了默认的设置<br></p>
<figure class="highlight jsp"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&lt;%@ page contentType=<span class="string">"text/html;charset=UTF-8"</span> language=<span class="string">"java"</span> %&gt;</span><br></pre></td></tr></table></figure>


      
    </div>

    
    
    
      <footer class="post-footer">
        <div class="post-eof"></div>
      </footer>
  </article>
  
  
  

        
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block home" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2020/03/04/Java%E8%B4%AA%E5%90%83%E8%9B%87%E5%BC%80%E5%8F%91/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="Roderick Xiang">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Roderick's blog">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          
            <a href="/2020/03/04/Java%E8%B4%AA%E5%90%83%E8%9B%87%E5%BC%80%E5%8F%91/" class="post-title-link" itemprop="url">Java贪吃蛇开发</a>
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2020-03-04 22:51:05" itemprop="dateCreated datePublished" datetime="2020-03-04T22:51:05+08:00">2020-03-04</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2020-02-16 22:01:36" itemprop="dateModified" datetime="2020-02-16T22:01:36+08:00">2020-02-16</time>
              </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
          <!-- TOC -->

<ul>
<li><a href="#一切先从核心玩法开始">一切先从核心玩法开始</a><ul>
<li><a href="#如何运动">如何运动</a></li>
<li><a href="#控制蛇头的方向">控制蛇头的方向</a></li>
</ul>
</li>
<li><a href="#游戏界面">游戏界面</a><ul>
<li><a href="#绘制游戏内容">绘制游戏内容</a></li>
<li><a href="#让一切都动起来">让一切都动起来</a></li>
<li><a href="#获取键盘事件">获取键盘事件</a></li>
</ul>
</li>
<li><a href="#游戏功能实现">游戏功能实现</a><ul>
<li><a href="#穿墙效果">穿墙效果</a></li>
<li><a href="#蛇的身子变长重点">蛇的身子变长（重点）</a></li>
<li><a href="#碰撞检测">碰撞检测</a></li>
<li><a href="#检测吃食物">检测吃食物</a></li>
<li><a href="#计分系统">计分系统</a></li>
</ul>
</li>
</ul>
<!-- /TOC -->
<h3 id="一切先从核心玩法开始"><a href="#一切先从核心玩法开始" class="headerlink" title="一切先从核心玩法开始"></a>一切先从核心玩法开始</h3><p>首先贪吃蛇的运动是发生在一个规范的矩阵中，而矩阵中的每一方格就是蛇的身子，而蛇的运动就是一次一次更新正方形的位置，然后通过panel绘制上去<br><br>所以我们使用两个数组来储存贪吃蛇的位置,snakeX用来储存横坐标，而snakeY用来储存纵坐标的位置<br></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//贪吃蛇初始的位置（一个头，两个身子）</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="title">Snake</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        snakeX[<span class="number">0</span>] = <span class="number">400</span>;</span><br><span class="line">        snakeY[<span class="number">0</span>] = <span class="number">400</span>;</span><br><span class="line">        snakeX[<span class="number">1</span>] = <span class="number">380</span>;</span><br><span class="line">        snakeY[<span class="number">1</span>] = <span class="number">400</span>;</span><br><span class="line">        snakeX[<span class="number">2</span>] = <span class="number">360</span>;</span><br><span class="line">        snakeY[<span class="number">2</span>] = <span class="number">400</span>;</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>
<h4 id="如何运动"><a href="#如何运动" class="headerlink" title="如何运动"></a>如何运动</h4><p>我们只要更新蛇的位置就能让它动起来了，所以我们定义两个速度”speedX”和”speedY”用来控制头的位置<br><br>只动了头并没有用，还有身子，我们只需要用从尾巴开始使用循环继承上一个身体的位置，知道继承头部位置<br></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">move</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="comment">//身体跟着头走</span></span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> i = length - <span class="number">1</span>; i &gt; <span class="number">0</span>; i--) &#123;</span><br><span class="line">        snakeX[i] = snakeX[i - <span class="number">1</span>];</span><br><span class="line">        snakeY[i] = snakeY[i - <span class="number">1</span>];</span><br><span class="line">    &#125;</span><br><span class="line">    snakeX[<span class="number">0</span>] += speedX;</span><br><span class="line">    snakeY[<span class="number">0</span>] += speedY;</span><br><span class="line">    isTmpMove = <span class="keyword">true</span>;</span><br><span class="line">    checkBoundaries();</span><br><span class="line">    System.out.format(<span class="string">"%d %d\n"</span>, snakeX[<span class="number">0</span>], snakeY[<span class="number">0</span>]);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h4 id="控制蛇头的方向"><a href="#控制蛇头的方向" class="headerlink" title="控制蛇头的方向"></a>控制蛇头的方向</h4><p>为了跟好的控制小蛇，直接控制蛇头来的最为直接。但是这里有一个会产生BUG的点，蛇头不能做反方向运动，所以我们还要给蛇头添加一个方向”direction”属性，只有当按键按下和方向条件满足时才能改变蛇头的运动方向<br></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">changeDirection</span><span class="params">(KeyEvent e)</span> </span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (e.getKeyCode() == KeyEvent.VK_UP &amp;&amp; !direction.equals(<span class="string">"down"</span>)) &#123;</span><br><span class="line">        direction = <span class="string">"up"</span>;</span><br><span class="line">        speedX = <span class="number">0</span>;</span><br><span class="line">        speedY = -<span class="number">20</span>;</span><br><span class="line">    &#125; <span class="keyword">else</span> <span class="keyword">if</span> (e.getKeyCode() == KeyEvent.VK_DOWN &amp;&amp; !direction.equals(<span class="string">"UP"</span>)) &#123;</span><br><span class="line">        direction = <span class="string">"down"</span>;</span><br><span class="line">        speedX = <span class="number">0</span>;</span><br><span class="line">        speedY = <span class="number">20</span>;</span><br><span class="line">    &#125; <span class="keyword">else</span> <span class="keyword">if</span> (e.getKeyCode() == KeyEvent.VK_RIGHT &amp;&amp; !direction.equals(<span class="string">"left"</span>)) &#123;</span><br><span class="line">        direction = <span class="string">"right"</span>;</span><br><span class="line">        speedX = <span class="number">20</span>;</span><br><span class="line">        speedY = <span class="number">0</span>;</span><br><span class="line">    &#125; <span class="keyword">else</span> <span class="keyword">if</span> (e.getKeyCode() == KeyEvent.VK_LEFT &amp;&amp; !direction.equals(<span class="string">"right"</span>)) &#123;</span><br><span class="line">        direction = <span class="string">"left"</span>;</span><br><span class="line">        speedX = -<span class="number">20</span>;</span><br><span class="line">        speedY = <span class="number">0</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    isTmpMove = <span class="keyword">false</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="游戏界面"><a href="#游戏界面" class="headerlink" title="游戏界面"></a>游戏界面</h3><p>游戏界面我们使用Jframe来实现（我好方便实现关闭功能）<br><br>写一个类类继承Jframe，在里面添加面板并且进行初始化设置<br></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MyFrame</span> <span class="keyword">extends</span> <span class="title">JFrame</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">init</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        JFrame frame = <span class="keyword">new</span> JFrame();</span><br><span class="line">        setTitle(<span class="string">"贪吃蛇"</span>);</span><br><span class="line">        setResizable(<span class="keyword">false</span>);</span><br><span class="line">        setBounds(<span class="number">300</span>,<span class="number">100</span>,<span class="number">865</span>,<span class="number">830</span>); <span class="comment">//840 *780 （界面的大小一定要设置正确）</span></span><br><span class="line">        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//添加Panel</span></span><br><span class="line">        GamePanel panel = <span class="keyword">new</span> GamePanel();</span><br><span class="line">        add(panel);</span><br><span class="line">        setVisible(<span class="keyword">true</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h4 id="绘制游戏内容"><a href="#绘制游戏内容" class="headerlink" title="绘制游戏内容"></a>绘制游戏内容</h4><p>已经添加了JPanal（GamePanel）,所以到Jpanal中添加游戏中需要绘制的内容<br><br>在Jpanal中重写paintComponent就能在实例化Jpanal时使用画笔绘制图像（方法自行被调用），这个类也是作为游戏的主类<br></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">MyPaint</span> <span class="keyword">extends</span> <span class="title">JPanel</span> </span>&#123;&#125;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">protected</span> <span class="keyword">void</span> <span class="title">paintComponent</span><span class="params">(Graphics g)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">super</span>.paintComponent(g);    <span class="comment">//清屏</span></span><br><span class="line">        g.setColor(Color.BLACK);</span><br><span class="line">        g.fillRect(<span class="number">100</span>,<span class="number">100</span>,<span class="number">100</span>,<span class="number">100</span>);</span><br><span class="line">        <span class="comment">//绘制所有的功能，省略不写......</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>所以有这个就能画小蛇了！<br></p>
<h4 id="让一切都动起来"><a href="#让一切都动起来" class="headerlink" title="让一切都动起来"></a>让一切都动起来</h4><p>为了有运动的效果，我们使用循环实现，这里我们使用ActionListener（函数式接口）用作定时器，我们实现一下接口里面的actionPerformed方法在方法里写需要定时执行的事件。定时器使用timer.start()开启<br></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//执行定时操作</span></span><br><span class="line"><span class="meta">@Override</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">actionPerformed</span><span class="params">(ActionEvent e)</span> </span>&#123;</span><br><span class="line">    snake.move();</span><br><span class="line">    snake.checkEatFood(food);</span><br><span class="line">    food.changeFoodPosition();  <span class="comment">//更改食物的位置</span></span><br><span class="line">    snake.checkCollide();   <span class="comment">//碰撞检测</span></span><br><span class="line">    repaint();  <span class="comment">//重新绘制图像</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h4 id="获取键盘事件"><a href="#获取键盘事件" class="headerlink" title="获取键盘事件"></a>获取键盘事件</h4><p>实现KeyListener接口来进行事件监听<br><br>使用之间还要设置监听<br></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">JPanal.setFocusable(<span class="keyword">true</span>); <span class="comment">//设置监听焦点</span></span><br><span class="line">Jpanal.addKeyListener(<span class="keyword">this</span>);   <span class="comment">//添加键盘监听事件</span></span><br><span class="line">接口中有三个抽象方法&lt;br&gt;</span><br></pre></td></tr></table></figure>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">keyPressed</span><span class="params">(KeyEvent e)</span> </span>&#123;</span><br><span class="line">    <span class="keyword">int</span> keycode = e.getKeyCode();</span><br><span class="line">    <span class="keyword">if</span> (keycode == KeyEvent.VK_SPACE) &#123;</span><br><span class="line">        isStart = !isStart;</span><br><span class="line">        <span class="keyword">if</span> (isStart) &#123;</span><br><span class="line">            timer.start();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125; <span class="keyword">else</span> <span class="keyword">if</span> (snake.isTmpMove) &#123;   <span class="comment">//判断小蛇是否移动，避免蛇头折回</span></span><br><span class="line">        snake.changeDirection(e);</span><br><span class="line">    &#125;</span><br><span class="line">    repaint();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="游戏功能实现"><a href="#游戏功能实现" class="headerlink" title="游戏功能实现"></a>游戏功能实现</h3><h4 id="穿墙效果"><a href="#穿墙效果" class="headerlink" title="穿墙效果"></a>穿墙效果</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">checkBoundaries</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (snakeX[<span class="number">0</span>] &gt; <span class="number">840</span>) &#123;</span><br><span class="line">        snakeX[<span class="number">0</span>] = <span class="number">0</span>;</span><br><span class="line">    &#125; <span class="keyword">else</span> <span class="keyword">if</span> (snakeX[<span class="number">0</span>] &lt; <span class="number">0</span>) &#123;</span><br><span class="line">        snakeX[<span class="number">0</span>] = <span class="number">840</span>;</span><br><span class="line">    &#125; <span class="keyword">else</span> <span class="keyword">if</span> (snakeY[<span class="number">0</span>] &gt; <span class="number">780</span>) &#123;</span><br><span class="line">        snakeY[<span class="number">0</span>] = <span class="number">0</span>;</span><br><span class="line">    &#125; <span class="keyword">else</span> <span class="keyword">if</span> (snakeY[<span class="number">0</span>] &lt; <span class="number">0</span>) &#123;</span><br><span class="line">        snakeY[<span class="number">0</span>] = <span class="number">780</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h4 id="蛇的身子变长（重点）"><a href="#蛇的身子变长（重点）" class="headerlink" title="蛇的身子变长（重点）"></a>蛇的身子变长（重点）</h4><p>我们让小蛇的尾部变长而不是头部，所以要参考蛇身最后的参数，并且获取速度<br><br>速度使用前坐标减后左边产生的差值得到<br><br>增加的位置通过蛇尾减速度得到<br></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">getLonger</span><span class="params">(Food food)</span> </span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (food.isEat) &#123;</span><br><span class="line">        Point.getNormalFoodPoint();</span><br><span class="line">        snakeX[length] = snakeX[length - <span class="number">1</span>] - (snakeX[length - <span class="number">2</span>] - snakeX[length - <span class="number">1</span>]);</span><br><span class="line">        snakeY[length] = snakeY[length - <span class="number">1</span>] - (snakeY[length - <span class="number">2</span>] - snakeY[length - <span class="number">1</span>]);</span><br><span class="line">        length++;</span><br><span class="line">    &#125; <span class="keyword">else</span> <span class="keyword">if</span> (food.isBonusEat) &#123;</span><br><span class="line">        Point.getBonusFoodPoint();</span><br><span class="line">        snakeX[length] = snakeX[length - <span class="number">1</span>] - (snakeX[length - <span class="number">2</span>] - snakeX[length - <span class="number">1</span>]);</span><br><span class="line">        snakeY[length] = snakeY[length - <span class="number">1</span>] - (snakeY[length - <span class="number">2</span>] - snakeY[length - <span class="number">1</span>]);</span><br><span class="line">        length++;</span><br><span class="line">        food.isBonusEat = <span class="keyword">false</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h4 id="碰撞检测"><a href="#碰撞检测" class="headerlink" title="碰撞检测"></a>碰撞检测</h4><p>遍历除了蛇头以外的位置,当坐标有相同的时候停止游戏<br></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">checkCollide</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i &lt; length; i++) &#123;</span><br><span class="line">        <span class="keyword">if</span> (snakeX[i] == snakeX[<span class="number">0</span>] &amp;&amp; snakeY[i] == snakeY[<span class="number">0</span>]) &#123;</span><br><span class="line">            GamePanel.isDead = <span class="keyword">true</span>;</span><br><span class="line">            System.out.format(<span class="string">"%d %d %d %d\n"</span>, snakeX[i], snakeY[i], snakeX[<span class="number">0</span>], snakeY[<span class="number">0</span>]);</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h4 id="检测吃食物"><a href="#检测吃食物" class="headerlink" title="检测吃食物"></a>检测吃食物</h4><p>判断头部位置是否与食物重叠就行<br><br>特殊食物还需要判断下是否存在<br></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">checkEatFood</span><span class="params">(Food food)</span> </span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (snakeX[<span class="number">0</span>] == food.normalX &amp;&amp; snakeY[<span class="number">0</span>] == food.normalY) &#123;</span><br><span class="line">        food.isEat = <span class="keyword">true</span>;</span><br><span class="line">        getLonger(food);</span><br><span class="line">    &#125; <span class="keyword">else</span> <span class="keyword">if</span> (snakeX[<span class="number">0</span>] == food.bonusX &amp;&amp; snakeY[<span class="number">0</span>] == food.bonusY &amp;&amp; food.hasBonus) &#123;</span><br><span class="line">        food.isBonusEat = <span class="keyword">true</span>;</span><br><span class="line">        food.hasBonus = <span class="keyword">false</span>;</span><br><span class="line">        getLonger(food);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h4 id="计分系统"><a href="#计分系统" class="headerlink" title="计分系统"></a>计分系统</h4><p>使用BufferReader和BufferWriter读写记录，获取最高分<br></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">writeResult</span><span class="params">()</span> <span class="keyword">throws</span> IOException </span>&#123;</span><br><span class="line">    BufferedWriter bw = <span class="keyword">new</span> BufferedWriter(<span class="keyword">new</span> FileWriter(<span class="string">"记录.txt"</span>, <span class="keyword">true</span>));</span><br><span class="line">    bw.write(<span class="string">"player"</span> + <span class="string">"\t"</span> + point);</span><br><span class="line">    bw.newLine();</span><br><span class="line">    bw.close();</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">readBestResult</span><span class="params">()</span> <span class="keyword">throws</span> IOException </span>&#123;</span><br><span class="line">    BufferedReader br = <span class="keyword">new</span> BufferedReader(<span class="keyword">new</span> FileReader(<span class="string">"记录.txt"</span>));</span><br><span class="line">    String line;</span><br><span class="line">    <span class="keyword">int</span> score;</span><br><span class="line">    <span class="keyword">while</span> ((line = br.readLine()) != <span class="keyword">null</span>) &#123;</span><br><span class="line">        String[] tmp = line.split(<span class="string">"\t"</span>);</span><br><span class="line">        score = Integer.parseInt(tmp[<span class="number">1</span>]);</span><br><span class="line">        <span class="keyword">if</span> (score &gt; bestScore) &#123;</span><br><span class="line">            bestScore = score;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
      
    </div>

    
    
    
      <footer class="post-footer">
        <div class="post-eof"></div>
      </footer>
  </article>
  
  
  

        
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block home" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2020/02/13/%E5%9C%A8%E8%A5%BF%E9%83%A8%E7%9A%84%E9%82%A3%E4%BA%9B%E6%97%A5%E5%AD%90/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="Roderick Xiang">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Roderick's blog">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          
            <a href="/2020/02/13/%E5%9C%A8%E8%A5%BF%E9%83%A8%E7%9A%84%E9%82%A3%E4%BA%9B%E6%97%A5%E5%AD%90/" class="post-title-link" itemprop="url">在西部的那些日子</a>
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>
              

              <time title="创建时间：2020-02-13 23:10:42 / 修改时间：23:09:36" itemprop="dateCreated datePublished" datetime="2020-02-13T23:10:42+08:00">2020-02-13</time>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
          <h1 id="在西部的那些日子"><a href="#在西部的那些日子" class="headerlink" title="在西部的那些日子"></a>在西部的那些日子</h1><p>&emsp;&emsp;对于荒野大镖客：救赎2的入坑有三点一是因为R星就是R星，另一部分是应为塞尔达旷野之息续作开发的消息谈到了，借鉴了荒野大镖客2中剧情对于整个游戏过程的重要性。剧情对于大表哥2来说是十分重要的，剧情发展对我来说通常是无所谓的，但是这次，也是唯一一次我在游戏中严重受到剧情的干扰，在玩之前不小心在B站云通关了，通常来说这没什么，但是对于荒野大镖客2来讲影响实在太大了。谁是叛徒，谁在哪里要领盒饭了，全都知道，太影响游戏体验，但是这应该不是我最后一次云通关，因为B站上剧情的诱惑力实在是太大了。<br><br>&emsp;&emsp;还有一点非常吸引我的毫无疑问是它的表现力，荒野大镖客2的表现力真的太强了，R星把range引擎发挥到了极致，创造出来了下一个世代的游戏，荒野大镖客2在ps4的末代发布，就像当年的gta5在ps3的末代发布一样，虽然pc版本跳了很久的票，但好歹摘了个“教科书般的优化”的游戏名号，也让千千万万平民玩家可以去洛圣都闯荡一下，当年也辛亏有这样的优化才能让我的gt920m能在25帧下“流畅”运行。荒野大镖客2的优化肯定是算不上好的，但是再怎么说也比gta4要强太多了，gta4在当年就是不想让你玩的存在，据说就连当年的神机都无法做到欢乐游玩，虽然我不太清楚哪个时代，但是今天的荒野大镖客2好歹能玩2080力大飞砖，而我的gtx1650也终于能打破cemu模拟器50%占用的枷锁（模拟器比较看重cpu,有来自cpu方面的瓶颈），但也只能在30帧下走中高了，优化就是中下的水平。但还有很多玩家遇到了根本无发愉快玩耍的情况（闪退，打不开之类的）。大表哥2在这方面倒是没太为难我，就是开始的时候虚拟内存设置太小了导致闪退。坐在马上看着阳光在树林中产生的丁达尔效应，在没有光线追踪的大表哥里难道还不满足吗？<br><br>&emsp;&emsp;在剧情上面荒野大镖客：救赎2其实是荒野大镖客：救赎1的前传，虽然玩2不需要1的剧情但其中联系还算比较紧密。在游戏的结尾上，2的处理方式与1如出一辙。R星在游戏的理念上认为开放世界就是要在结束任务后还能继续对世界进行探索，所以在亚瑟完成他的救赎之路后，还需要使用约翰完成复仇就像在1中约翰完成成他的救赎之后，需要杰克完成复仇一样。当你在2中看着陌生的约翰时就像1中看着陌生的杰克一样。而荒野之息在结尾上的处理就感觉很有问题。<br><br>&emsp;&emsp;荒野大镖客：救赎2在国内给我的感觉就是典型的叫好不叫座，可能是因为我们这边对于西部文化不感冒，或许又是因为大多数玩家太过于浮躁，完全无法理解这种慢节奏的，骑马模拟器游戏。在所有人都嚷嚷着要gta6的时候，荒野大镖客：救赎2并没有得到应有的拥护度，gta5还在卖呢！哦我的上帝你可瞧瞧吧，那可是13年的游戏啊。</p>

      
    </div>

    
    
    
      <footer class="post-footer">
        <div class="post-eof"></div>
      </footer>
  </article>
  
  
  

        
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block home" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2020/02/07/%E6%95%B0%E7%BB%84/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="Roderick Xiang">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Roderick's blog">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          
            <a href="/2020/02/07/%E6%95%B0%E7%BB%84/" class="post-title-link" itemprop="url">数组</a>
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2020-02-07 21:37:03" itemprop="dateCreated datePublished" datetime="2020-02-07T21:37:03+08:00">2020-02-07</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2020-01-09 17:12:13" itemprop="dateModified" datetime="2020-01-09T17:12:13+08:00">2020-01-09</time>
              </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
          <p>[TOC]</p>
<h4 id="数组声明创建"><a href="#数组声明创建" class="headerlink" title="数组声明创建"></a>数组声明创建</h4><h5 id="声明数组"><a href="#声明数组" class="headerlink" title="声明数组"></a>声明数组</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">dataType[] array;  <span class="comment">//首选方法</span></span><br><span class="line">dataType array[];    <span class="comment">//C语言适应方法</span></span><br></pre></td></tr></table></figure>
<h5 id="创建数组"><a href="#创建数组" class="headerlink" title="创建数组"></a>创建数组</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">dataType[] array = <span class="keyword">new</span> dataType[arraySize]  <span class="comment">//一步到位</span></span><br></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> array;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">ArrayDemon01</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span>[] numbers;</span><br><span class="line">        numbers = <span class="keyword">new</span> <span class="keyword">int</span>[<span class="number">3</span>];</span><br><span class="line">        numbers[<span class="number">0</span>] = <span class="number">1</span>;</span><br><span class="line">        numbers[<span class="number">1</span>] = <span class="number">2</span>;</span><br><span class="line">        numbers[<span class="number">2</span>] = <span class="number">3</span>;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> x : numbers) &#123;</span><br><span class="line">            System.out.println(x);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h5 id="初始化"><a href="#初始化" class="headerlink" title="初始化"></a>初始化</h5><p>1.静态初始化<br><br>2.动态初始化</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> array;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Demon02</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="comment">//静态初始化：创建 + 初始化</span></span><br><span class="line">        <span class="keyword">int</span>[] a = &#123;<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>&#125;;</span><br><span class="line"></span><br><span class="line">        <span class="comment">//动态初始化：包含默认初始化</span></span><br><span class="line">        <span class="keyword">int</span>[] b = <span class="keyword">new</span> <span class="keyword">int</span>[<span class="number">5</span>];</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h5 id="数组高级遍历"><a href="#数组高级遍历" class="headerlink" title="数组高级遍历"></a>数组高级遍历</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">for</span> (<span class="keyword">int</span> x : array)&#123;</span><br><span class="line">    System.out.println(x);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
      
    </div>

    
    
    
      <footer class="post-footer">
        <div class="post-eof"></div>
      </footer>
  </article>
  
  
  

        
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block home" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2020/02/07/pip%E5%91%BD%E4%BB%A4/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="Roderick Xiang">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Roderick's blog">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          
            <a href="/2020/02/07/pip%E5%91%BD%E4%BB%A4/" class="post-title-link" itemprop="url">pip命令</a>
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2020-02-07 21:37:03" itemprop="dateCreated datePublished" datetime="2020-02-07T21:37:03+08:00">2020-02-07</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2020-01-04 20:49:32" itemprop="dateModified" datetime="2020-01-04T20:49:32+08:00">2020-01-04</time>
              </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
          <h4 id="安装包"><a href="#安装包" class="headerlink" title="安装包"></a>安装包</h4><p>pip install &lt;包名&gt;</p>
<h4 id="查看已安装的包"><a href="#查看已安装的包" class="headerlink" title="查看已安装的包"></a>查看已安装的包</h4><p>pip list</p>
<h4 id="查看可升级的的包"><a href="#查看可升级的的包" class="headerlink" title="查看可升级的的包"></a>查看可升级的的包</h4><p>pip list -o</p>
<h4 id="升级"><a href="#升级" class="headerlink" title="升级"></a>升级</h4><p>pip install -U &lt;包名&gt;</p>
<h4 id="卸载包"><a href="#卸载包" class="headerlink" title="卸载包"></a>卸载包</h4><p>pip uninstall &lt;包名&gt;</p>
<h4 id="指定单次安装源"><a href="#指定单次安装源" class="headerlink" title="指定单次安装源"></a>指定单次安装源</h4><p>pip install &lt;包名&gt; -i <a href="https://mirrors.aliyun.com/pypi/simple" target="_blank" rel="noopener">https://mirrors.aliyun.com/pypi/simple</a></p>
<h4 id="显示包所在的位置"><a href="#显示包所在的位置" class="headerlink" title="显示包所在的位置"></a>显示包所在的位置</h4><p>pip show &lt;包名&gt;</p>

      
    </div>

    
    
    
      <footer class="post-footer">
        <div class="post-eof"></div>
      </footer>
  </article>
  
  
  

        
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block home" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2020/02/07/hello-world/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="Roderick Xiang">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Roderick's blog">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          
            <a href="/2020/02/07/hello-world/" class="post-title-link" itemprop="url">Hello World</a>
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>
              

              <time title="创建时间：2020-02-07 21:37:03 / 修改时间：17:27:18" itemprop="dateCreated datePublished" datetime="2020-02-07T21:37:03+08:00">2020-02-07</time>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
          <p>Welcome to <a href="https://hexo.io/" target="_blank" rel="noopener">Hexo</a>! This is your very first post. Check <a href="https://hexo.io/docs/" target="_blank" rel="noopener">documentation</a> for more info. If you get any problems when using Hexo, you can find the answer in <a href="https://hexo.io/docs/troubleshooting.html" target="_blank" rel="noopener">troubleshooting</a> or you can ask me on <a href="https://github.com/hexojs/hexo/issues" target="_blank" rel="noopener">GitHub</a>.</p>
<h2 id="Quick-Start"><a href="#Quick-Start" class="headerlink" title="Quick Start"></a>Quick Start</h2><h3 id="Create-a-new-post"><a href="#Create-a-new-post" class="headerlink" title="Create a new post"></a>Create a new post</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ hexo new <span class="string">"My New Post"</span></span><br></pre></td></tr></table></figure>

<p>More info: <a href="https://hexo.io/docs/writing.html" target="_blank" rel="noopener">Writing</a></p>
<h3 id="Run-server"><a href="#Run-server" class="headerlink" title="Run server"></a>Run server</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ hexo server</span><br></pre></td></tr></table></figure>

<p>More info: <a href="https://hexo.io/docs/server.html" target="_blank" rel="noopener">Server</a></p>
<h3 id="Generate-static-files"><a href="#Generate-static-files" class="headerlink" title="Generate static files"></a>Generate static files</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ hexo generate</span><br></pre></td></tr></table></figure>

<p>More info: <a href="https://hexo.io/docs/generating.html" target="_blank" rel="noopener">Generating</a></p>
<h3 id="Deploy-to-remote-sites"><a href="#Deploy-to-remote-sites" class="headerlink" title="Deploy to remote sites"></a>Deploy to remote sites</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ hexo deploy</span><br></pre></td></tr></table></figure>

<p>More info: <a href="https://hexo.io/docs/one-command-deployment.html" target="_blank" rel="noopener">Deployment</a></p>

      
    </div>

    
    
    
      <footer class="post-footer">
        <div class="post-eof"></div>
      </footer>
  </article>
  
  
  

  </div>

  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">Roderick Xiang</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">7</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/roderickxiang" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;roderickxiang" rel="noopener" target="_blank"><i class="fa fa-fw fa-github"></i>GitHub</a>
      </span>
  </div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Roderick Xiang</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> 强力驱动 v4.2.0
  </div>
  <span class="post-meta-divider">|</span>
  <div class="theme-info">主题 – <a href="https://theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Gemini</a> v7.7.2
  </div>

        








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>




  













<script>
if (document.querySelectorAll('pre.mermaid').length) {
  NexT.utils.getScript('//cdn.jsdelivr.net/npm/mermaid@8/dist/mermaid.min.js', () => {
    mermaid.initialize({
      theme    : 'forest',
      logLevel : 3,
      flowchart: { curve     : 'linear' },
      gantt    : { axisFormat: '%m/%d/%Y' },
      sequence : { actorMargin: 50 }
    });
  }, window.mermaid);
}
</script>


  

  

</body>
</html>
